class Solution {
public:
    int finalValueAfterOperations(vector<string>& operations) {
        int ret = 0;
        stack<char> stk;
        for (const auto& elem : operations) {
            for (const auto& ch : elem) {
                if (ch == 'X') {
                    continue;
                }
                if (stk.empty() || ch == stk.top()) {
                    stk.push(ch);
                } else {
                    stk.pop();
                }
            }
        }

        if (!stk.empty()) {
            ret = stk.size() / 2;
            if (stk.top() == '-') {
                ret = -ret;
            }
        }

        return ret;
    }
};